/*

File name		: table5.do

Description		: creates table on action-based vs motivation-based persistence
									
Output File		: table5.tex
				
*/

set more off, perm
capture restore

*** 001 Analysis/Tabulation ***
use "data/blood_donation/blood_donation.dta", clear

	// Merge rainfall data
	merge m:1 entnahmedatum using "data/rainfall/rainfallZH.dta"
	*, keep(match)

	*rename kalendertag daily_rainfall
	rename daily_rainfall_ZH daily_rainfall
	gen any_rain = 1 if daily_rainfall > 0 & daily_rainfall != .
	replace any_rain = 0 if daily_rainfall == 0
	sort spnr entnahmedatum
	gen any_rain_plus1 = any_rain[_n+1] if spnr == spnr[_n+1]

	sort spnr entnahmedatum
	gen rainfall_plus1 = daily_rainfall[_n+1] if spnr == spnr[_n+1]
	gen strong_rain = 1 if daily_rainfall >= 10
	replace strong_rain = 0 if daily_rainfall < 10

	// Robustnesss: Extreme heat and cold. 
	gen extreme_cold = 1 if daily_high_ZH < 5 & daily_high_ZH != .
	replace extreme_cold = 0 if daily_high_ZH >= 5 & daily_high_ZH != .

	replace daily_low_ZH = "." if daily_low_ZH == "-"
	destring daily_low_ZH, replace
	gen frost_day = 1 if daily_low_ZH < 0 & daily_low_ZH != .
	replace frost_day = 0 if daily_low_ZH >= 0 & daily_low_ZH != .

	gen extreme_heat = 1 if daily_high_ZH > 25 & daily_high_ZH != .
	replace extreme_heat = 0 if daily_high_ZH <= 25 & daily_high_ZH != .

	gen adverse_weather = 0 if extreme_cold != .
	replace adverse_weather = 1 if extreme_cold == 1 | extreme_heat == 1 | strong_rain == 1
	
	// Generate Controls
	gen spring=1 if period==1|period==3
	replace spring=0 if spring==.
	gen month=month(entnahmedatum)
	gen week=week(entnahmedatum)
	tab month, gen(month_dummy)
	tab c_num, gen(c_num_dummy)

	label define order 1 "K" 2 "1a" 3 "1b" 4 "2"
	encode treatment, gen(treatment_ordered) label(order)
	label values treatment_ordered order
	
	// Generate lag terms
	sort spnr period
	gen don_minus1=don[_n-1] if period>1&spnr==spnr[_n-1]
	gen don_minus2=don[_n-2] if period>2&spnr==spnr[_n-2]
	gen don_minus3=don[_n-3] if period>3&spnr==spnr[_n-3]

	// Generate lead terms
	gen don_plus1=don[_n+1] if period<4&spnr==spnr[_n+1]
	gen don_plus2=don[_n+2] if period<3&spnr==spnr[_n+2]
	gen don_plus3=don[_n+3] if period<2&spnr==spnr[_n+3]

	gen phonecall_plus1=phonecall[_n+1] if period<4&spnr==spnr[_n+1]

	sort spnr entnahmedatum
	gen adverse_weather_plus1=adverse_weather[_n+1] if spnr==spnr[_n+1]
	
	lab var don "\$\widehat{Donation_{t-1}}\$"
	lab var adverse_weather "Adv. Weather\$ _{t-1}\$"
	lab var phonecall_plus1 "Call\$ _{t}\$"
	lab var phonecall "Call\$ _{t-1}\$"
	lab var adverse_weather_plus1 "Adv. Weather\$ _{t}\$"
	
/*	
	preserve
		
		drop if don == . | adverse_weather_plus1 == . | phonecall_plus1 == . |  adverse_weather == . | phonecall == . | don_plus1 == .
		
		bysort spnr c_num: gen id = _n
		
		export delimited using /Users/dorothytws/Library/CloudStorage/Dropbox/Typ2-Studie/JPEMicro_replication/data/clean/blood_donation/donation_tomatlab.csv, replace
			
	restore 
*/

drop if don == . | adverse_weather_plus1 == . | phonecall_plus1 == . |  adverse_weather == . | phonecall == . | don_plus1 == .
	
	/* Regression */
	// Column 1:
	ivreghdfe don_plus1 (don = phonecall) adverse_weather adverse_weather_plus1 ///
	phonecall_plus1 bltype_dummy2 bltype_dummy3 male age, ///
	cluster(c_num spnr) absorb(week) first savefirst
	
	estimates restore _ivreg2_don
	
	outreg2 using  output/tables/table5, tex replace ///
	ctitle("1\$ ^{st}\$ stage", "Donation\$ _{t-1}\$") ///
	addtext(Instrument in the 1\$ ^{st}\$ stage, \, Kleibergen/Paap F-statistic, \, ///
	Sargan-Hansen J-test p-val, \ , Observations \, `e(N)') ///
	drop(male age bltype_dummy2 bltype_dummy3) ///
	nor2 noobs nocons
	
	// Column 2:
	ivreghdfe don_plus1 (don = adverse_weather phonecall) adverse_weather_plus1 ///
	phonecall_plus1 bltype_dummy2 bltype_dummy3 male age, ///
	cluster(c_num spnr) absorb(week) first savefirst
	*est store stage2
	*est dir
	*est replay _ivreg2_don
	
	cap drop don_plus1_hat
	predict don_plus1_hat, xb
	cap drop residual
	gen residual = don_plus1 - don_plus1_hat
	
	/* 
	
	Note: For the 2SLS estimator, the test statistic is Sargan's statistic, 
		  typically calculated as N*R-squared from a regression of the IV 
		  residuals on the full set of instruments. 
	
	reg residual phonecall adverse_weather ///
	phonecall_plus1 adverse_weather_plus1 ///
	bltype_dummy2 bltype_dummy3 male age
	
	scalar N = `e(N)'*`e(r2)'
	scalar num = e(N)*e(r2)
	display chi2(1, num)
	
	*/
		
	
	/*
		
	mkmat adverse_weather phonecall phonecall_plus1 adverse_weather_plus1 bltype_dummy2 bltype_dummy3 male age, matrix(Xexog)
	matrix Xexogt = Xexog'
	matrix P = Xexog*inv(Xexogt*Xexog)*Xexogt
	mkmat residual, matrix(u)
	matrix ut = u'
	matrix s = (ut*P*u)
	matrix s2 = (ut*u)
	scalar s3 = s[1,1]/s2[1,1]
	display 1-chi2(1, s3)
	
	*/
	
	/* cap drop residual_hat
		predict residual_hat, xb
		cap drop residual_e
		gen residual_e = residual - residual_hat
		mkmat residual_e, matrix(e)
		matrix et = e'
		
		matrix e2 = (et*e)
		scalar e3 = e2[1,1]/s2[1,1]
		scalar num2 = e(N)*(1-e3)
		display chi2(1, num2)
	*/
	

// ---------------------------------	
	local kp: di %4.2f `e(widstat)'
	local sh: di %4.3f `e(jp)'
	
	outreg2 using  output/tables/table5, ///
	tex append ///
	ctitle("Donation\$ _{t}\$") ///
	addtext(Instrument in the 1\$ ^{st}\$ stage, Both, Kleibergen/Paap F-statistic, `kp', ///
	Sargan-Hansen J-test p-val, `sh' , Observations \, `e(N)') ///
	drop(male age bltype_dummy2 bltype_dummy3) ///
	nor2 noobs nocons
	
	
	// Column 3:
	ivreghdfe don_plus1 (don = phonecall) adverse_weather_plus1 phonecall_plus1 ///
	bltype_dummy2 bltype_dummy3 male age, ///
	cluster(c_num spnr) absorb(week) first savefirst
	
	local kp: di %4.2f `e(widstat)'
	
	outreg2 using  output/tables/table5, ///
	tex append ///
	ctitle("Donation\$ _{t}\$") ///
	addtext(Instrument in the 1\$ ^{st}\$ stage, Call, Kleibergen/Paap F-statistic, `kp', ///
	Sargan-Hansen J-test p-val, \ , Observations \, `e(N)') ///
	drop(male age bltype_dummy2 bltype_dummy3) ///
	nor2 noobs nocons
		
	// Column 4:
	ivreghdfe don_plus1 (don = adverse_weather) adverse_weather_plus1 ///
	phonecall_plus1 bltype_dummy2 bltype_dummy3 male age, ///
	cluster(c_num spnr) absorb(week) first savefirst
		
	local kp: di %4.2f `e(widstat)'
	
	outreg2 using  output/tables/table5, ///
	tex append ///
	ctitle("2\$ ^{nd}\$ stages", "Donation\$ _{t}\$") ///
	addtext(Instrument in the 1\$ ^{st}\$ stage, Adv. Weather, ///
	Kleibergen/Paap F-statistic, `kp', ///
	Sargan-Hansen J-test p-val, \ , Observations \, `e(N)') ///
	drop(male age bltype_dummy2 bltype_dummy3) ///
	nor2 noobs nocons
	
	// Column 5:
	ivreghdfe don_plus1 (don = phonecall) adverse_weather adverse_weather_plus1 ///
	phonecall_plus1 bltype_dummy2 bltype_dummy3 male age, ///
	cluster(c_num spnr) absorb(week) first savefirst
		
	local kp: di %4.2f `e(widstat)'
	
	outreg2 using  output/tables/table5, ///
	tex append ///
	ctitle("Donation\$ _{t}\$") ///
	addtext(Instrument in the 1\$ ^{st}\$ stage, Call, ///
	Kleibergen/Paap F-statistic, `kp', ///
	Sargan-Hansen J-test p-val, \ , Observations \, `e(N)') ///
	drop(male age bltype_dummy2 bltype_dummy3) ///
	nor2 noobs nocons
	
	// Column 6:
	ivreghdfe don_plus1 (don = adverse_weather) adverse_weather_plus1 ///
	phonecall phonecall_plus1 bltype_dummy2 bltype_dummy3 male age, ///
	cluster(c_num spnr) absorb(week) first savefirst
		
	local kp: di %4.2f `e(widstat)'
	
	outreg2 using output/tables/table5, ///
	tex(frag) append ///
	ctitle("Donation\$ _{t}\$") ///
	addtext(Instrument in the 1\$ ^{st}\$ stage, Adv. Weather, ///
	Kleibergen/Paap F-statistic, `kp', ///
	Sargan-Hansen J-test p-val, \ , Observations \, `e(N)') ///
	drop(male age bltype_dummy2 bltype_dummy3) ///
	sortvar(don phonecall adverse_weather phonecall_plus1 adverse_weather_plus1) ///
	nor2 noobs nocons ///
	label
	
